﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace SwormTests.IUD
{
    public class update_include_and_exclude : TestBase
    {
        public update_include_and_exclude(DbToTest db) : base(db) { }

        class simple
        {
            [Sworm.Column(IsPrimaryKey = true, IsAutoIncrement = false)]
            public long id { get; set; }
            public string str_col { get; set; }
            public int int_col { get; set; }
        }

        [Test]
        public void can_exclude_columns()
        {
            var record = new simple() { id = 1, str_col = "one", int_col = 1 };
            DB.Insert(record);
            record = DB.Query.Simple<simple>().Single(1);
            Assert.AreEqual("one", record.str_col);
            Assert.AreEqual(1, record.int_col);

            record.str_col = "two";
            record.int_col = 2;
            DB.Update(record, options => options.Exclude(x => x.int_col));
            record = DB.Query.Simple<simple>().Single(1);
            Assert.AreEqual("two", record.str_col);
            Assert.AreEqual(1, record.int_col);

            record.str_col = "three";
            record.int_col = 3;
            DB.Update(record, options => options.Exclude("StR_cOl"));
            record = DB.Query.Simple<simple>().Single(1);
            Assert.AreEqual("two", record.str_col);
            Assert.AreEqual(3, record.int_col);
        }

        [Test]
        public void can_include_columns()
        {
            var record = new simple() { id = 94, str_col = "one", int_col = 1 };
            DB.Insert(record);
            record = DB.Query.Simple<simple>().Single(94);
            Assert.AreEqual("one", record.str_col);
            Assert.AreEqual(1, record.int_col);

            record.str_col = "two";
            record.int_col = 2;
            DB.Update(record, options => options.Include(x => x.str_col));
            record = DB.Query.Simple<simple>().Single(94);
            Assert.AreEqual("two", record.str_col);
            Assert.AreEqual(1, record.int_col);

            record.str_col = "three";
            record.int_col = 3;
            DB.Update(record, options => options.Include("InT_Col"));
            record = DB.Query.Simple<simple>().Single(94);
            Assert.AreEqual("two", record.str_col);
            Assert.AreEqual(3, record.int_col);

            record.str_col = "four";
            record.int_col = 4;
            DB.Update(record, options => options.Include("sTr_col", "Int_COl"));
            record = DB.Query.Simple<simple>().Single(94);
            Assert.AreEqual("four", record.str_col);
            Assert.AreEqual(4, record.int_col);

            record.str_col = "five";
            record.int_col = 5;
            DB.Update(record, options => options.Include(x => x.str_col, x => x.int_col));
            record = DB.Query.Simple<simple>().Single(94);
            Assert.AreEqual("five", record.str_col);
            Assert.AreEqual(5, record.int_col);
        }
    }
}
